package kr.co.bettersoft.sendme.provider;

import java.util.ArrayList;

import kr.co.bettersoft.sendme.activity.SendMeBlockListActivity;
import kr.co.bettersoft.sendme.activity.SendMeDownloadMessageListActivity;
import kr.co.bettersoft.sendme.util.SendMeCommon;
import kr.co.bettersoft.sendme.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SendMeBlockAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<SendMeBlock> sendMeBlockList;
	
	
	private SendMeBlock sendMeBlock;
	LayoutInflater layoutInflater;
	
	private SendMeBlockListActivity receiveSendMeBlockListActivity;
	
	SendMeCommon sendMeCommon;
	
	String TAG = "SendMeDownloadMessageListActivity";
	
	View view;
	
	ImageButton updateBlockBtn;
	
	public SendMeBlockAdapter(Context context, SendMeBlockListActivity receiveSendMeBlockListActivity, ArrayList<SendMeBlock> sendMeBlockList) {
		this.context = context;
		this.sendMeBlockList = sendMeBlockList;
		this.receiveSendMeBlockListActivity = receiveSendMeBlockListActivity;
		
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		sendMeCommon = new SendMeCommon();
	}
	

	@Override
	public int getCount() {
		if(sendMeBlockList!=null){
			return sendMeBlockList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return sendMeBlockList.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.activity_send_me_block_list_adapter, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		sendMeBlock = sendMeBlockList.get(position);
		
		
		updateBlockBtn = (ImageButton) view.findViewById(R.id.updateBlockBtn);
		updateBlockBtn.setTag(position);

		updateBlockBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			int selectedPosition = Integer.parseInt(v.getTag()+"");
//			receiveSendMeBlockListActivity.updateBlockUser(selectedPosition);
			receiveSendMeBlockListActivity.updateBlockUser(sendMeBlockList.get(selectedPosition));
//			receiveTempMessageListActivity.cancelPosition = Integer.parseInt(v.getTag()+"");
//			receiveTempMessageListActivity.confirmAndCancelUser();
			}
		});
//		downloadListSendBtn.setOnClickListener(new OnClickListener() {
//		@Override
//		public void onClick(View v) {
//			
//			int selectedPosition = Integer.parseInt(v.getTag()+"");
//			receiveSendMeDownloadMessageListActivity.sendAudioMessage(selectedPosition);
//			}
//		});

//		// 친구 이름을 세팅 한다.
		wrapper.getTxtViewFriendName().setText(sendMeBlock.getNickName());
		// 친구 전화번호 세팅 한다.	  
		wrapper.getTxtViewFriendPhoneNumber().setText(sendMeCommon.phoneNumberForShowFormat(sendMeBlock.getPhoneNumber()));
		// 메시지 등록일 표시
		wrapper.getRegisterDate().setText(sendMeBlock.getRegisterDate());
				
		return view;
	}
	
	class ViewWrapper {
		View base;
		
		TextView txtViewFriendName;				// 친구 이름.
		TextView txtViewFriendPhoneNumber;				// 친구 전화번호.
//		ImageButton imageButtonSendCheck;		// 보낼 사람 선택.
//		
		TextView registerDate;					// 메시지 등록 일
		
		ViewWrapper(View base) {
			this.base = base;
		}
//		
		TextView getTxtViewFriendName() {
			if(txtViewFriendName == null) {
				txtViewFriendName = (TextView)view.findViewById(R.id.txtViewFriendName);
			}
			
			return txtViewFriendName;
		}

		TextView getTxtViewFriendPhoneNumber() {
			if(txtViewFriendPhoneNumber == null) {
				txtViewFriendPhoneNumber = (TextView)view.findViewById(R.id.txtViewFriendPhoneNumber);
			}
			
			return txtViewFriendPhoneNumber;
		}
		
//		ImageButton getImageButtonSendCheck() {
//			if(imageButtonSendCheck == null) {
//				imageButtonSendCheck = (ImageButton)view.findViewById(R.id.imageButtonSendCheck);
//			}
//
//			return imageButtonSendCheck;
//		}
		
		TextView getRegisterDate() {
			if(registerDate == null) {
				registerDate = (TextView)view.findViewById(R.id.registerDate);
			}
			
			return registerDate;
		}
	}

}